<?php

namespace app\admin\controller\depot;

use app\common\controller\Backend;
use think\exception\PDOException;
use Exception;
use think\Db;

// 供应商
class Supplier extends Backend
{
    protected $model = null;

    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();

        $this->model = model("Depot.Supplier");
    }
    
    // 供应商列表
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if($this->request->isAjax())
        {
            if ($this->request->request('keyField')) 
            {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with(['provinces','citys','districts'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
                ->with(['provinces','citys','districts'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }
    
    // 添加供应商
    public function add()
    {
        if($this->request->isPost())
        {
            $params = $this->request->param('row/a');

            if(is_array($params) || !empty($params))
            {
                $result = $this->model->validate('common/Depot/Supplier')->save($params);

                if($result === FALSE)
                {
                    $this->error($this->model->getError());
                    exit;
                }else
                {
                    $this->success();
                    exit;
                }
            }
        }

        return $this->view->fetch();
    }

    // 编辑供应商
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);

        if(!$row)
        {
            $this->error(__('该供应商不存在，请重新选择'));
            exit;
        }

        if($this->request->isPost())
        {
            $params = $this->request->post('row/a');

            if($params)
            {
                $params['id'] = $ids;

                $result = $this->model->validate('common/Depot/Supplier')->isUpdate(true)->save($params);

                if($result === FALSE)
                {
                    $this->error($this->model->getError());
                    exit;
                }else
                {
                    $this->success();
                    exit;
                }
            }
        }

        $this->assign('row', $row);

        return $this->view->fetch();
    }

    // 删除供应商
    public function del($ids = null)
    {
        $ids = !empty($ids) ?  explode(',', $ids) : [];

        $row = $this->model->select($ids);

        if(!$row)
        {
            $this->error(__('没有找到该来源'));
            exit;
        }

        $result = $this->model->destroy($ids);

        if ($result === false) 
        {
            $this->error($this->model->getError());
            exit;
        }else
        {
            $this->success();
            exit;
        }
    }
}